function [s] = make_struct(varargin) 
% (name, instring)
%
% make a structure with the  name in n (string)
% with the fields v (strings)
% and the corresponding data assigned from d. 
% typical use:
% convert a string of the following form to a structure:
% 'del 5, np 10, freq 20'
% to s.del = 5, s.np = 10, s.freq = 20
%
% useful for passing specific, identified parameter values to analysis
% routines from the main Control sheet in datac. 
% string might come from the "Comments" field, or from the "Parameters"
% field.
%
% 9/25/2008. Paul B. Manis
%

s=[];
if(nargin < 1)
    fprintf(1, 'make_struct: expect at least instring\n');
    return;
end;

instring = varargin{1};
if(~ischar(instring))
    fprintf(1, 'make_struct: needs a string for the structure  data\n');
    return;
end;

if(nargin == 2)
    defaults = varargin{2}; % allow a set of defaults to be passed
    try
        [dfields, ddati] = strread(defaults, '%s %d,'); % get the defaults
    catch
        fprintf(1, 'make_struct: Error in parsing defaults: %s\n', defaults)
        return;
    end;
    for i = 1:length(dfields) % go ahead and set the defaults
        eval(sprintf('s.%s = %f;',  char(dfields{i}), ddati(i)));
    end;
end;

% now parse the incoming string. 

try
    [fields, dati] = strread(instring, '%s %d,');
catch
    fprintf(1, 'make_struct: Error in parameter string: %s \n', instring);
    fprintf(1, '             must be of form: a 5, b 10... \n');
    return;
end;
 % load up the structure. Note that any defaults will be overwritten if
 % a field with the same name exists; also fields not defined will be
 % added.
 
for i = 1:length(fields)
    eval(sprintf('s.%s = %f;',  char(fields{i}), dati(i)));
end;


    
    